package com.mku.parkingmanagement_service.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mku.parkingmanagement_service.entity.User;
import com.mku.parkingmanagement_service.service.UserService;
import com.mku.parkingmanagement_service.vo.UserVo;
import com.mku.parkingmanagement_service.vo.comm.ResultVO;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @className: UserController
 * @author: Maoxx
 * @date: 2024/11/7 22:48
 * @Version: 1.0
 * @description:
 */

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param user 用户信息对象
     */
    @PostMapping("/login")
    public ResultVO<Object> login(@RequestBody User user) {
        return userService.login(user);
    }


    /**
     * 注册
     * @param user 用户信息对象
     * @return
     */
    @PostMapping("/register")
    public ResultVO<Object> register(@RequestBody User user) {
        User one = userService.getOne(new QueryWrapper<User>().eq("username", user.getUsername()));
        if (one != null) {
            return ResultVO.fail("用户名已存在");
        }
        return userService.register(user);
    }


    @PostMapping("/checkUsername/{username}")
    public ResultVO<Object> checkUsername(@PathVariable("username") String username) {
        return ResultVO.success(userService.getOne(new QueryWrapper<User>().eq("username", username))!= null);
    }

    @GetMapping("/getUsers")
    public ResultVO<Object> getUsers(UserVo userVo) {
        return ResultVO.success(userService.getUsersPage(userVo));
    }


    @PostMapping("/search")
    public ResultVO<Object> search(@RequestBody UserVo userVo) {
        return ResultVO.success(userService.getUsersPage(userVo));
    }


    @PutMapping("/editUser")
    public ResultVO<Object> editUser(@RequestBody User user) {
        boolean update = userService.updateById(user);
        if (!update) {
            return ResultVO.fail("修改失败", null);
        }
        return new ResultVO<>().message("修改成功").flag(true);
    }

    @DeleteMapping("/deleteUser/{uid}")
    public ResultVO<Object> deleteUser(@PathVariable("uid") Integer uid) {
        boolean remove = userService.removeById(uid);
        if (!remove) {
            return ResultVO.fail("删除失败,未找到该车主", null);
        }
        return new ResultVO<>().message("删除成功").flag(true);
    }


    @PutMapping("/rePassword/{uid}/{password}")
    public ResultVO<Object> rePassword(@PathVariable("uid") Integer uid, @PathVariable("password") String password) {
        return userService.rePassword(uid, password);
    }

    @GetMapping("/dataType")
    public ResultVO<Object> dataType(String person) {
        return ResultVO.success(userService.getUserDataType(person));
    }

    @GetMapping("/dataMoney")
    public ResultVO<Object> dataMoney(String person) {
        return ResultVO.success(userService.getUserDataMoney(person));
    }


    @PostMapping("/userPay")
    public ResultVO<Boolean> userPay(@RequestBody User user){
        return ResultVO.success(userService.userPay(user));
    }
}
